﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FinWeb3.Models.ModelView;
using System.Security.Principal;
using FinWeb3.Models.Helpers;

namespace FinWeb3.Models
{
    public partial class Bill
    {
        public decimal Value
        {
            get
            {
                if (HasDetailList)
                {
                    decimal value = 0M;

                    foreach (var line in DetailLines)
                    {
                        value += line.Value;
                    }

                    return value;
                }
                else
                {
                    return BaseValue;
                }
            }
            set
            {
                if (!HasDetailList)
                {
                    BaseValue = value;
                }
            }
        }

        public Dictionary<string, decimal> GetCategoriesTotals()
        {
            var categoryTotals = new Dictionary<string, decimal>();

            if (HasDetailList)
            {
                foreach (var line in DetailLines)
                {
                    if (!categoryTotals.Keys.Contains(line.Category))
                    {
                        categoryTotals.Add(line.Category, 0M);
                    }

                    categoryTotals[line.Category] += line.Value;
                }
            }
            else
            {
                categoryTotals.Add(Category, BaseValue);
            }

            return categoryTotals;
        }

        public List<Bill> DetailLines { get; set; }

        //partial void OnLoaded()
        //{
        //    this.Description = "decripted! - " + this.Description;
        //}
    }
}
